#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "test_kit.h"

int rcg_fd = -1;

static char Ssrv_addr[] = {"172.26.18.106"};
static char Send_response[] = {"Beamreader>"};

unsigned short calculate_CRC( unsigned short Type,
									  const unsigned char *pData,
									  unsigned long Len,
									  unsigned short CRC )
{
	unsigned short XorConst;
	unsigned char  BitCtr, BitCheck, DataByte;

	if( Type == CRC16_WORD )
	{
		XorConst = CRC16_WORD;
	}
	else
	{
		XorConst = CRC_CCITT_WORD;
	}

	while( Len-- )
	{
		DataByte = *pData++;

		for( BitCtr = 8; BitCtr; BitCtr-- )
		{
			BitCheck = (unsigned char)(CRC & 1);
			CRC >>= 1;
			BitCheck ^= DataByte & 1;
			DataByte >>= 1;

			if( BitCheck )
			{
				CRC ^= XorConst;
			}
		}
	}

	return( CRC );
}


int connect_to_rcg(void)
{
	struct sockaddr_in my_addr_in,srv_addr_in;
	int ret;
	/*use IPv4,tcp*/
	rcg_fd = socket(PF_INET,SOCK_STREAM,0);
	while(rcg_fd < 0){
		rcg_fd = socket(PF_INET,SOCK_STREAM,0);
	}
//	debug_screen("rcg_fd:%d\n",rcg_fd);
	my_addr_in.sin_family = PF_INET;
	my_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
	my_addr_in.sin_port = htons(0);
	ret = bind(rcg_fd,(struct sockaddr *)&my_addr_in,sizeof(my_addr_in));
	while(ret<0){
		ret = bind(rcg_fd,(struct sockaddr *)&my_addr_in,sizeof(my_addr_in));
	}
//	debug_screen("bind:%d\n",ret);
//	inet_aton(Ssrv_addr,&srv_addr);
	srv_addr_in.sin_family = PF_INET;
	srv_addr_in.sin_addr.s_addr = inet_addr(Ssrv_addr);
	srv_addr_in.sin_port = htons(RCG_APP_LISTEN_PORT);
	ret = connect(rcg_fd, (struct sockaddr *)&srv_addr_in,sizeof(srv_addr_in));
//	debug_screen("connect:%d\n",ret);
	return 0;
}/*connect_to_rcg*/

int disconnect_rcg(void)
{
	if(rcg_fd > 0)
	{
		close(rcg_fd);
	}
	return 0;
}/*connect_to_rcg*/


/*
* function		: get_statistics
* author			: uni.chen
* create date		: 08/07/2009
* para			:
					statistics(out) 	: one line of statistics
					len(in)			: the statistics' buffer length
					cmd(in)			: start or next
* return			: 
					<0	: error
					0 	: This is the last frame and there is NO statistics any more.
					>0 	: the statistics' length
* description		: Get statistics of RCG app and Wirama one by one.
				  This function will ONLY get one statistics at a time, if you
				  want to get more statistics, please call this function with 
				  GET_STATISTICS_NEXT cmd.
				  And when you want to get statistics from the beginning, please
				  call this function with GET_STATISTICS_START and then with 
				  GET_STATISTICS_NEXT until there is no more statistics(return is 0).
*/
int get_statistics(char *statistics, int len, int cmd)
{
	return 0;
}/*get_statistics*/

/*
* function		: get_rcg_wirama_settings
* author			: uni.chen
* create date		: 08/07/2009
* para			:
					setting(out) 	: one line of settings
					len(in)		: the length of setting's buffer
					cmd(in)		: start or next
* return			: 
					<0 	: error
					0   	: This is the last frame and there is NO settings any more.
					>0 	: the settings' length
* description		: Get settings of RCG app and Wirama one by one.
				  This function will ONLY get one setting at a time, if you
				  want to get more settings, please call this function with 
				  GET_SETTINGS_NEXT cmd.
				  And when you want to get settings from the beginning, please
				  call this function with GET_SETTINGS_START and then with 
				  GET_SETTINGS_NEXT until there is no more statistics(return is 0).
*/
int get_rcg_wirama_settings(char *setting, int len, int cmd)
{
	return 0;
}/*get_rcg_wirama_settings*/

int send_wirama_command(const char *command)
{
/*	debug_screen("begin send_wirama_command:%s,rcg_fd:%d\n",command,rcg_fd);
*/
	if(rcg_fd > 0)
	{
		return send(rcg_fd,command,strlen(command)+1,0);
	}
/*	debug_screen("end send_wirama_command\n");
*/
	return -1;
}/*send_wirama_command*/

/*
* function		: get_wirama_response
* author			: uni.chen
* create date		: 08/07/2009
* para			:
					response(out) 	: one line of response
					len(in)			: the response's buffer length
* return			: 
					<0 	: error
					0 	: This is the last response and there is NO response any more.
					>0 	: the response's length
* description		: Get response from wirama line by line.
* Attention		: Please call send_wirama_command before calling this command.
*/
int get_wirama_response(char *response,int len)
{
	int ret;
	if(rcg_fd > 0)
	{
		ret = read(rcg_fd,response,len-1);
		if(NULL == strstr(response,Send_response))
		{/*There is no end string*/
			return ret;
		}
		return 0;
	}
	return -1;
}/*get_wirama_response*/


